//
//  FirmCell.m
//  Hxcsfw
//
//  Created by qiysoft on 2018/1/29.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "FirmCell.h"

@implementation FirmCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.BView1.layer.masksToBounds = YES;
    self.BView1.layer.cornerRadius = 5;
    self.BView1.layer.borderWidth = 1;
    self.BView1.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView2.layer.masksToBounds = YES;
    self.BView2.layer.cornerRadius = 5;
    self.BView2.layer.borderWidth = 1;
    self.BView2.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView3.layer.masksToBounds = YES;
    self.BView3.layer.cornerRadius = 5;
    self.BView3.layer.borderWidth = 1;
    self.BView3.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView4.layer.masksToBounds = YES;
    self.BView4.layer.cornerRadius = 5;
    self.BView4.layer.borderWidth = 1;
    self.BView4.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView5.layer.masksToBounds = YES;
    self.BView5.layer.cornerRadius = 5;
    self.BView5.layer.borderWidth = 1;
    self.BView5.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.OKBtn.layer.masksToBounds = YES;
    self.OKBtn.layer.cornerRadius = 5;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)OKBtn:(id)sender {//企业注册确定按钮
    
    self.OKBtn.enabled = NO;//登录禁点
    if(![self checkInput] ) {
        return;
    }
    
    /*
     nsrsbhTF;
     qymcTF;
     phoneTF;
     mimaTF;
     mimaTF2;
     OKBtn
     */
    
    [self requestNsrsbh:self.nsrsbhTF.text qymcTF:self.qymcTF.text phoneTF:self.phoneTF.text mimaTF:self.mimaTF.text mimaTF2:self.mimaTF2.text];
    
}

//企业用户注册请求
- (void)requestNsrsbh:(NSString *)nsrsbhTF qymcTF:(NSString *)qymcTF phoneTF:(NSString *)phoneTF mimaTF:(NSString *)mimaTF mimaTF2:(NSString *)mimaTF2{
    
    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"regist",@"content":@{@"revert":@"1",@"dto":@{@"username":self.nsrsbhTF.text,@"nsrmc":self.qymcTF.text,@"phone":self.phoneTF.text,@"password":self.mimaTF.text,@"confirmpwd":self.mimaTF2.text}}};
    
    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};
    
    
    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];
        
        if ([successStr boolValue]) {//true
            [SVProgressHUD showImage:nil status:@"注册成功"];
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
            [SVProgressHUD dismissWithDelay:1.];
            [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];   //背景颜色
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"FirmRegistSuccess" object:nil];
//            [WShareUtil sharedData].dicRegist = returnDic;//注册信息单例
            
        } else {
           
            //注册失败原因
            [WShareUtil showAlert:@"温馨提示" message:[returnDic objectForKey:@"errorInfo"]];
        }
        self.OKBtn.enabled = YES;
        
    } failure:^(NSError *error) {
        [SVProgressHUD showImage:nil status:@"注册失败"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
        [SVProgressHUD dismissWithDelay:1.];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        self.OKBtn.enabled = YES;
        
    } showProgress:YES];
}

- (IBAction)YesOrNoBtn:(id)sender {//是否同意协议
    [_delegate XieYi:sender];
}

- (IBAction)agreementBtn:(id)sender {//打开跳转协议
    [_delegate DaKaiXieYi:sender];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

//判断登录条件
#pragma mark - check input
-(BOOL)checkInput{
    
    BOOL resultBool = NO;
    
    //----------------------是否同意注册协议
    if (self.YesOrNoBtn.selected) {//没同意协议
        [WShareUtil showAlert:@"温馨提示" message:@"未阅读并同意注册协议！"];
        self.OKBtn.enabled = YES;
        return resultBool;
    }
    
    //----------------------纳税人识别号
    NSString *strNsrsbhTF = self.nsrsbhTF.text;
    if ([WShareUtil strNilOrEmpty:strNsrsbhTF]) {
        [WShareUtil showAlert:@"温馨提示" message:@"纳税人识别号不能为空！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    strNsrsbhTF = [strNsrsbhTF stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strNsrsbhTF]) {
        [WShareUtil showAlert:@"温馨提示" message:@"纳税人识别号不能全部为空格！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    //----------------------企业名称
    if ([WShareUtil strNilOrEmpty:self.qymcTF.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"企业名称不能为空！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    NSString *strqymcTF = self.qymcTF.text;
    strqymcTF = [strqymcTF stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strqymcTF]) {
        [WShareUtil showAlert:@"温馨提示" message:@"企业名称不能全部为空格！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    //----------------------手机号
    if ([WShareUtil strNilOrEmpty:self.phoneTF.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"手机号不能为空！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    NSString *strPhoneTF = self.phoneTF.text;
    strPhoneTF = [strPhoneTF stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strPhoneTF]) {
        [WShareUtil showAlert:@"温馨提示" message:@"手机号不能全部为空格！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    //----------------------密码1
    if ([WShareUtil strNilOrEmpty:self.mimaTF.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"注册密码不能为空！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    NSString *strmimaTF = self.mimaTF.text;
    strmimaTF = [strmimaTF stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strmimaTF]) {
        [WShareUtil showAlert:@"温馨提示" message:@"注册密码不能全部为空格！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    //----------------------密码2
    if ([WShareUtil strNilOrEmpty:self.mimaTF2.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"确认注册密码不能为空！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    NSString *strmimaTF2 = self.mimaTF2.text;
    strmimaTF2 = [strmimaTF2 stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strmimaTF2]) {
        [WShareUtil showAlert:@"温馨提示" message:@"确认注册密码不能全部为空格！"];
        self.OKBtn.enabled = YES;
        //        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    return YES;
}

@end
